package com.ceba.test.modules.admin.service;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.ceba.base.enums.SealTypeEnum;
import com.ceba.base.helper.LoggerHelper;
import com.ceba.base.utils.ImageHelper;
import com.ceba.cebautils.StringUtil;
import com.ceba.test.base.enums.StampTypeEnum;
import com.ceba.test.base.language.ResourceUtils;
import org.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.ceba.test.base.configs.CebaPathUtil;
import com.ceba.base.web.service.IBaseService;
import com.ceba.test.modules.admin.dao.StampDao;
import com.ceba.test.modules.admin.entity.GStamp;
import com.ceba.test.modules.email.entity.MsgMail;

import abc.ID;
import abc.data.DataTable;


/**
 * 印章图片等地址管理
 *
 */
public class StampService extends IBaseService{
	
	private static Logger logger = LoggerFactory.getLogger(StampService.class);
	
	private  StampDao stampDao;

	
	/**
	 * 根据id获取章对象
	 * @return
	 * @throws Exception
	 */
	public GStamp getStampById(String id)throws Exception{
		return stampDao.getStampById(id);
		
	}
	
	
	public String getFirstStampIdByUserId(String userId) {
		GStamp stamp = stampDao.getFirstStampIdByUserId(userId);
		if(stamp !=null ){
			return stamp.getId();
		}
		return "";
	}

	public  GStamp getStampObjectDataTable() {
		return stampDao.getStampObjectDataTable();
	}
	
	/**
	 * 增加章配置
	 * @param g
	 * @return
	 */
	public boolean  addStamp(GStamp g){
		return stampDao.addStamp(g);
	}
	
	/**
	 * 更新章配置
	 * @param g
	 * @return
	 */
	public boolean  updateStamp(GStamp g){
		return stampDao.updateStamp(g);
	}
	/**
	 * 获取个人章对象
	 * @return
	 * @throws Exception
	 */
	public  GStamp getUserStampObjectByUserID(String userId) throws Exception{
		return stampDao.getUserStampObjectByUserID(userId);
	}
	
	
	/**
	 * 获取公章对象
	 * @return
	 * @throws Exception
	 */
	public  GStamp getCorStampObjectByStampId(String stampId) throws Exception{
		return stampDao.getCorStampObjectByStampId(stampId);
	}
	
	/**
	 * 获取可选的报告章
	 * @return
	 */
	@SuppressWarnings("rawtypes")
	public  List<GStamp> getSelectCorStamps(String userId) throws Exception{
		return stampDao.getSelectCorStamps(userId);
	}
	
	
	
	/**
	 * 获取可选的报告章
	 * @return
	 */
	public  String getSelectCorStampsStrings(String userId) throws Exception{
		return  stampDao.getSelectCorStampsStrings(userId);
	}
	/**
	 * 获取用户个人章
	 * @param stampID
	 * @return
	 * @throws Exception
	 */
	public List<GStamp> getUserGStampById(String stampID) throws Exception{
		return stampDao.getUserGStampById(stampID);
	}
	
	
	public List<GStamp>  getCorStampsObjectByUserId(String userId,String corType,String authorization) throws Exception{
		return stampDao.getCorStampsByUserId(userId,corType,authorization);
	}
	

	public List<GStamp>  getCorStampsObject(String corType) throws Exception{
		return stampDao.getCorStamp(corType);
	}
	
	/**
	 * 关联用户公章
	 * @param userId
	 * @param gStampId
	 * @return
	 */
	public boolean addUserCorStamp(String userId,String gStampId,int type){
		
		return stampDao.addUserCorStamp(userId, gStampId,type);
	}		
	

	
	/**
	 * 用户公章和用户是否在g_user_cor_stamp表中已经关联
	 * created by dingf
	 * @param userId
	 * @param gStampId
	 * @return
	 */
	public boolean isUserCorStampConnected(String userId,String gStampId){
		DataTable dt=stampDao.isUserCorStampConnected(userId, gStampId);
		if(dt !=null && dt.getRowCount()>0){
			return true;
		}else{
			return false;
		}
	}
	
	/**
	 * 用户公章和用户是否在g_user_cor_stamp表中已经关联
	 * created by dingf
	 * @param userId
	 * @return
	 */
	public boolean canCorStampDelete(String userId){
		DataTable dt=stampDao.canCorStampDelete(userId);
		if(dt.getRowCount()<1){
			return true;
		}else{
			return false;
		}
	}
	
	
	public boolean delUserCorStamp(String userId){
	
		return stampDao.delUserCorStamp(userId);
	}
	
	/**
	 * 删除报告章,从g_stamp表和g_user_cor_stamp表，
	 * 同时删除记录。
	 * @return
	 */
	public boolean deleteCorStampV1(String id){
		return stampDao.deleteCorStampV1(id);
	}
	
	public boolean delUserCorStampByUserId(String userId){
	
		return stampDao.delUserCorStampByUserId(userId);
	}

	//从根据USER_ID从g_stamp表中取出所有记录的ID,COR_STAMP_NAME
	public List<GStamp>  getUserStampDTByUserId(String userId) throws Exception {
		return stampDao.getUserStampDTByUserId(userId);
	}
	
	
	/**
	 * 获取可选的用户章
	 * created by dingf
	 * @return
	 */
	@SuppressWarnings("static-access")
	public  String getSelectedUserStampsStrings(String userId) throws Exception{
		return stampDao.getSelectedUserStampsStrings(userId);
	}
	
	/*
	 * 根据章名称获取章
	 */
	public GStamp getStampByStampName(String stampName,int type){
		return stampDao.getStampByStampName(stampName,type);
	}
	
	/**
	 * 根据用户章名称判断章是否存在
	 * @param userStampName
	 * @return
	 */
	public boolean checkUserStampNameExist(String userStampName,String colFlag,String id)  throws Exception{
		return stampDao.checkUserStampNameExist(userStampName, colFlag,id);
	}
	
	/**
	 * 判断是否有多个草稿水印
	 * @param 
	 * @return
	 */
	public boolean checkWarterExist()  throws Exception{
		return stampDao.checkWarterExist();
	}
	
	/**
	 * 获取可选的报告章
	 * created by dingf
	 * @return
	 */
	@SuppressWarnings("static-access")
	public String getSelectCorStampsStringsVersion1(String userId) throws Exception{
		
		return stampDao.getSelectCorStampsStringsVersion1(userId);
	}
	
	/**
	 * 获取单个用户的授权签章
	 * @return
	 */
	public JSONObject queryAuthorizeStamp(String userId,String corFlag,Map<String,Object> dic) {
		return stampDao.queryAuthorizeStamp(userId,corFlag,dic);
	}


	public List<GStamp> getMultiInterfaceSigns() {
		
		return stampDao.getMultiInterfaceSigns();
	}
	
	
	/**
	 * 获取用户的COR_STAMP_NAME
	 * @param 
	 * @return
	 */
	public String getUserCorStampName(String ID){
		return stampDao.getUserCorStampName(ID);
	}


	/**
	 * 根据id获取章对象
	 * @return
	 * @throws Exception
	 */
	public  List<GStamp> getStampsListByIds(Map<String, Object> paramMap)throws Exception{
		StringBuffer g_StampBuffer=new StringBuffer();
		String g_StampIds=(String) paramMap.get("g_stempIds");
		if(g_StampIds.indexOf(",")!=-1){
			for(String g_Stamp:g_StampIds.split(",")){
				g_StampBuffer.append("'"+g_Stamp+"',");
			}
			g_StampIds=g_StampBuffer.toString().substring(0, g_StampBuffer.length()-1);
		}else{
			g_StampIds="'"+g_StampIds+"'";
		}
		
		List<GStamp> g_stampList= stampDao.getStampsListByIds(g_StampIds);
	  return   g_stampList;
	}
	
	
	/**
	 * 根据id获取骑缝章对象
	 * @return
	 * @throws Exception
	 */
	public  List<GStamp> getStampsQiFengListByIds(Map<String, Object> paramMap)throws Exception{
		StringBuffer g_StampBuffer=new StringBuffer();
		String g_StampIds=(String) paramMap.get("g_stempIds");
		if(g_StampIds.indexOf(",")!=-1){
			for(String g_Stamp:g_StampIds.split(",")){
				g_StampBuffer.append("'"+g_Stamp+"',");
			}
			g_StampIds=g_StampBuffer.toString().substring(0, g_StampBuffer.length()-1);
		}else{
			g_StampIds="'"+g_StampIds+"'";
		}
		
		List<GStamp> g_stampList= stampDao.getStampsListByIds(g_StampIds);
		List<GStamp> returnStampList = new ArrayList();
		if(g_stampList !=null && g_stampList.size()>0){
			for(int i=0;i<g_stampList.size();i++){
				GStamp gs = g_stampList.get(i);
				
				gs.setId(ID.getGuid());
				gs.setFilePath(CebaPathUtil.getVirtualUpfilePathName()+gs.getFilePath());
				gs.setCorFlag(StampTypeEnum.QIFENG_COR_STAMP_FLAG.getType());
				returnStampList.add(gs);
			}
			return returnStampList;
		}else{
			return null;
		}
	}

	/**
	 * 获取水印地址
	 * @return
	 */	
	public  GStamp getWatermark(String qianWaterId,String waterType) {
		try{
			GStamp gStamp = null;
			if(StringUtil.isEmptyString(qianWaterId)){
				gStamp = getFirstWaterStampByCondition(" COR_FLAG = '"+waterType+"'");
				if(gStamp != null){
					return gStamp;
				}
				return null;
			}
			gStamp = getStampById(qianWaterId);
			if(gStamp !=null){
				return  gStamp;
			}
		}catch(Exception e){
			LoggerHelper.error(e, "");
		}
		return null;
	}
	

	/*
	 * 根据条件获取第一条水印
	 */
	public GStamp getFirstWaterStampByCondition(String condition) {
		return stampDao.getFirstWaterStampByCondition(condition);
	}


	/**
	 * 获取水印列表--签名水印和草稿水印
	 * @param orderBy
	 * @param key
	 * @param dic
	 * @return
	 */
	public JSONObject getWatermarkList(String orderBy, String key, Map<String,Object> dic,String WATERMARK_COR_STAMP_FLAG) {
		return stampDao.getWatermarkList(orderBy, key,dic,WATERMARK_COR_STAMP_FLAG);
	}
	
	/**
	 * 根据ID删除
	 * @param stampId
	 * @return
	 */
	public boolean delStampById(String stampId){
		return stampDao.delStampById(stampId);
	}
	
	/**
	 * 根据查询条件获取章列表
	 * @param condition
	 * @return
	 */
	public List<GStamp> getGStampListByCondition(String condition){
		return stampDao.getGStampListByCondition(condition);
	}

	
	/**
	 * 获取用户印章授权列表
	 */
	public  JSONObject getUserDigitalStampList(String key,int limit,int start) throws Exception{
		DataTable dt = stampDao.getUserDigitalStampList(key,limit,start);
		return dt.toJSONObject();
	}
	public  long getUserDigitalStampCount(String key,int limit,int start) throws Exception{
		return stampDao.getUserDigitalStampCount(key,limit,start);
	}

	
	/**
	 * 获取用户印章授权列表
	 */
	public  DataTable getUserDigitalStampPicList(String userId,int type) throws Exception{
		DataTable dtPic = stampDao.getUserDigitalStampPicList(userId,type);
		return dtPic;
	}

	public GStamp queryDigitalStampCerNO(){
		return stampDao.queryDigitalStampCerNO();
	}
	
	
	
	/**
	 * 获取其他章列表
	 * 章列表
	 * @param
	 * @return
	 */
	public  JSONObject getStampList(String corFlag,String orderBy,Map<String,Object> dic) throws Exception{
		JSONObject jsonObj1 = stampDao.getCorStampList(orderBy,corFlag, dic);
		return jsonObj1;
	}

	public  JSONObject getWaterStapList(String corFlag,String orderBy,Map<String,Object> dic) throws Exception{
		return stampDao.getWaterStapList(orderBy,corFlag, dic);
	}
	public MsgMail getBase(){
		return stampDao.getBase();
	}
	
	public String deleteDir(String id){
		return stampDao.deleteDir(id);
	}

	public  StampDao getStampDao() {
		return stampDao;
	}


	public  void setStampDao(StampDao stampDao) {
		this.stampDao = stampDao;
	}

}
